<?php
/*
 * TODO: This need some sort of security key adding to prevent unwated adding to the data tables
 */
require('sanitize.inc.php');
//PARANOID, SQL, SYSTEM, HTML, INT, FLOAT, LDAP, UTF8 
//echo sanitize($Test, $Flags); 
//error_log("==== START ====/n");
//var_dump($_GET);
/* Connect to an ODBC database using driver invocation */


$dsn = 'mysql:dbname=weather;host=127.0.0.1';
$user = 'weatheruser';
$password = 'dragon32';

$dateutc = "";
$softwaretype = "";
$tempf = "";
$temp = "";
$windspeedmph = "";
$baromin = "";
$barom = "";
$rainin = "";
$rain = "";
$humidity = "";
$winddir = "";
$windgustmph = "";
$dewptf = "";
$dewpt = "";
$dailyrainin = "";
$dailyrain = "";

try {
//	error_log("Trying to Connect");
    $dbh = new PDO($dsn, $user, $password);
} catch (PDOException $e) {
    echo 'Connection failed: ' . $e->getMessage();
    die("Died in Connection");
}
if (isset($_GET['dateutc']))
    $dateutc = sanitize($_GET['dateutc'], SQL);#   2013-04-24+07%3A29%3A05
if (isset($_GET['softwaretype']))
    $softwaretype = sanitize($_GET['softwaretype'], PARANOID);#  pywws
if (isset($_GET['tempf'])) {
    $tempf = sanitize($_GET['tempf'], FLOAT); #  54.5
    $temp = convertFar2Deg($tempf);
}
if (isset($_GET['windspeedmph']))
    $windspeedmph = sanitize($_GET['windspeedmph'], FLOAT);#  3.80
if (isset($_GET['baromin'])) {
    $baromin = sanitize($_GET['baromin'], FLOAT); #  30.1472
    $barom = convertInches2Hpa($baromin);
}
if (isset($_GET['rainin'])) {
    $rainin = sanitize($_GET['rainin'], FLOAT); #  0.0590551
    $rain = convertInches2mm($rainin);
}
if (isset($_GET['humidity']))
    $humidity = sanitize($_GET['humidity'], INT);#  74
if (isset($_GET['winddir'])) {
    $winddir = sanitize($_GET['winddir'], INT); #  315
    if ($windspeedmph == 0)
        $winddir = 0;
}else {
    $winddir = 0;
}
if (isset($_GET['windgustmph']))
    $windgustmph = sanitize($_GET['windgustmph'], FLOAT);#  6.04
if (isset($_GET['key']))
    $key = sanitize($_GET['key'], PARANOID);#  ILOVESAMANTHA
if (isset($_GET['dewptf'])) {
    $dewptf = sanitize($_GET['dewptf'], FLOAT); #  46.4
    $dewpt = convertFar2Deg($dewptf);
}
if (isset($_GET['dailyrainin'])) {
    $dailyrainin = sanitize($_GET['dailyrainin'], FLOAT); #   0.0826772
    $dailyrain = convertInches2mm($dailyrainin);
}
$sql = "INSERT INTO raw (dateutc, softwaretype, temp, windspeedmph, barom, rain, humidity, winddir, windgustmph, dewpt, dailyrain) VALUES (:dateutc, :softwaretype, :temp, :windspeedmph, :barom, :rain, :humidity, :winddir, :windgustmph, :dewpt, :dailyrain);";

try {
    $sth = $dbh->prepare($sql);
//	error_log("Prpare Run");
//	if($sth) error_log("Prepare OK");
} catch (PDOException $e) {
    echo 'Connection failed: ' . $e->getMessage();
    die("Died in Prepare");
}

try {
    $sth->execute(
            array(
                ':dateutc' => $dateutc,
                ':softwaretype' => $softwaretype,
                ':temp' => $temp,
                ':windspeedmph' => $windspeedmph,
                ':barom' => $barom,
                ':rain' => $rain,
                ':humidity' => $humidity,
                ':winddir' => $winddir,
                ':windgustmph' => $windgustmph,
                ':dewpt' => $dewpt,
                ':dailyrain' => $dailyrain
            )
    );
} catch (PDOException $e) {
    echo 'Connection failed: ' . $e->getMessage();
    die("Died in execute");
}

function convertFar2Deg($f) {
    //Deduct 32, then multiply by 5, then divide by 9
    $d = (($f - 32) * 5) / 9;
    return $d;
}

function convertInches2Hpa($inM) {
    //X inches of mercury * cf = Y hectopascals
    $hPa = $inM * 33.86389;
    return $hPa;
}

function convertInches2mm($inch) {
    $mm = $inch * 25.4;
    return $mm;
}

//error_log("==== END ====/n");
